package serviceGoods

import (
	"context"
	goods_pb "mxshop/api/goods/v1"
	"mxshop/app/mxshop_api/api/internal/data"
	dtoGoods "mxshop/app/mxshop_api/api/internal/domain/dto/goods"
	srvGoods "mxshop/app/mxshop_api/api/internal/service/goods"
)

type CategoryService struct {
	data data.DataFactory
}

func (c CategoryService) GetAllCategorysList(ctx context.Context) (*dtoGoods.CategoryDTOList, error) {
	var ret dtoGoods.CategoryDTOList

	rsp, err := c.data.Goods().Category().AllList(ctx)
	if err != nil {
		return nil, err
	}

	for _, v := range rsp.Items {
		ret.Items = append(ret.Items, &dtoGoods.CategoryDTO{CategoryDO: *v})
	}
	ret.JsonData = rsp.JsonData

	return &ret, nil
}

func (c CategoryService) GetCategorysList(ctx context.Context, req *goods_pb.CategoryListRequest) (*dtoGoods.CategoryDTOList, error) {
	var ret dtoGoods.CategoryDTOList

	rsp, err := c.data.Goods().Category().List(ctx, req)
	if err != nil {
		return nil, err
	}

	for _, v := range rsp.Items {
		ret.Items = append(ret.Items, &dtoGoods.CategoryDTO{CategoryDO: *v})
	}

	return &ret, nil
}

func (c CategoryService) GetSubCategory(ctx context.Context, req *goods_pb.CategoryListRequest) (*dtoGoods.CategoryDTO, error) {
	var ret dtoGoods.CategoryDTO

	rsp, err := c.data.Goods().Category().SubList(ctx, req)
	if err != nil {
		return nil, err
	}

	ret.CategoryDO = *rsp

	return &ret, nil
}

func (c CategoryService) CreateCategory(ctx context.Context, req *dtoGoods.CategoryDTO) (*dtoGoods.CategoryDTO, error) {
	var ret dtoGoods.CategoryDTO

	rsp, err := c.data.Goods().Category().Create(ctx, &req.CategoryDO)
	if err != nil {
		return nil, err
	}

	ret.Id = rsp.Id

	return &ret, nil
}

func (c CategoryService) DeleteCategory(ctx context.Context, ID int64) error {
	return c.data.Goods().Category().Delete(ctx, ID)
}

func (c CategoryService) UpdateCategory(ctx context.Context, req *dtoGoods.CategoryDTO) error {
	return c.data.Goods().Category().Update(ctx, &req.CategoryDO)
}

func newCategory(data data.DataFactory) srvGoods.CategorySrv {
	return &CategoryService{data: data}
}
